<?php
define('ROOT', '../..');
require ROOT . '/lib/includeForBlog.php';
if (false) {
	fetchConfigVal();
}

# Locale language file support, defualt is en-us.php
if (!isset($blog['blogLanguage'])) {
	$blog['blogLanguage'] = $service['language'];
}

if(empty($pluginPath)) $pluginPath = ROOT.'/plugins/HC_signuptoy';
if(isset($__ts)) unset($__ts);
$__ts = array();
// Locale language file support.
switch($blog['blogLanguage']) {
	case "zh-TW":
	case "zh-CN":
		$languageFile = $pluginPath.'/lang/'.$blog['blogLanguage'].'.php';
	break;
	default:
		$languageFile = "";
}
if(!empty($languageFile) && file_exists($languageFile)) { include ($languageFile); }
if (file_exists($pluginPath.'/lang/pluginLocal.php')) require_once ($pluginPath.'/lang/pluginLocal.php');

function HC_AddSignUpBlogForm() {
	list($signuphtml,$signupcss,$signupscript) = HC_SignUpSetting();
	
	ob_start();
?>
	<style tyle="text/css">
	<!--
	<?=$signupcss;?>
	-->
	</style>
	<script language="javascript" type="text/javascript">
	/*<![CDDA[*/
	<?=$signupscript;?>
	/*]]>*/
	</script>
	<!-- Textcube OutLogin Form Start -->
	<form method="post" onsubmit="return false;">
		<input type="hidden" name="requiresid" value="<?php echo session_id();?>" />
		<?=$signuphtml;?>
	</form>
	<!-- Textcube OutLogin Form End -->
<?php
	$temp = ob_get_contents();
	ob_end_clean();

	return $temp;
}

function HC_AddSignUpTeamBlogForm($target) {
	list($signuphtml,$signupcss) = SignUpSetting('teamblog');
}

function HC_SignUpSetting() {
	global $database, $userid, $blogid;
	
	ob_start();
	?>
	<div class="signupform">
		<h3><?php echo pluginLang::plts('多人部落格申請');?></h3>
		<dl>
			<dt><label for="blogloginid"><?php echo pluginLang::plts('登入帳號(e-mail)');?></label></dt>
			<dd><input type="text" class="input-text" id="blogloginid" name="blogloginid" value="" maxlength="64" size="25" /></dd><br class="clear" />
			<dt><label for="blogpassword"><?php echo pluginLang::plts('登入密碼');?></label></dt>
			<dd><input type="password" class="input-text" id="blogpassword" name="blogpassword" maxlength="64" size="25" /></dd><br class="clear" />
			<dt><label for="blogre-password"><?php echo pluginLang::plts('確認密碼');?></label></dt>
			<dd><input type="password" class="input-text" id="blogre-password" name="blogre-password" maxlength="64" size="25" /></dd><br class="clear" />
			<dt><label for="blogname"><?php echo pluginLang::plts('部落格識別名稱');?></label></dt>
			<dd><input type="text" class="input-text" id="blogname" name="blogname" maxlength="32" size="25" /></dd><br class="clear" />
			<dt><label for="username"><?php echo pluginLang::plts('使用者名稱');?></label></dt>
			<dd><input type="text" class="input-text" id="username" name="username" maxlength="32" size="25" /></dd><br class="clear" />
		</dl>
		<div class="button-box"><input type="button" class="login-button input-button" value="<?php echo pluginLang::plts('確認送出');?>" onclick="multiblogsignup();" /></div>
		<br />
		<h3><?php echo pluginLang::plts('團隊部落格申請');?></h3>
		<dl>
			<dt><label for="teamblogselect"><?php echo pluginLang::plts('選擇部落格');?></label></dt>
			<dd><select id="teamblogselect" name="teamblogselect" size="1">
	<?php
	$rows = DBQuery::queryAll("SELECT blogid, value FROM {$database['prefix']}BlogSettings WHERE name='title' GROUP BY blogid");
	foreach( $rows as $row ) {
		echo '<option label="'.$row['value'].'" value="'.$row['blogid'].'">'.$row['value'].'</option>';
	}
	?>
			</select></dd><br class="clear" />
			<dt><label for="teamloginid"><?php echo pluginLang::plts('登入帳號(e-mail)');?></label></dt>
			<dd><input type="text" class="input-text" id="teamloginid" name="teamloginid" value="" maxlength="64" size="25" /></dd><br class="clear" />
			<dt><label for="password"><?php echo pluginLang::plts('登入密碼');?></label></dt>
			<dd><input type="password" class="input-text" id="teampassword" name="teampassword" maxlength="64" size="25" /></dd><br class="clear" />
			<dt><label for="re-password"><?php echo pluginLang::plts('確認密碼');?></label></dt>
			<dd><input type="password" class="input-text" id="teamre-password" name="teamre-password" maxlength="64" size="25" /></dd><br class="clear" />
			<dt><label for="teamusername"><?php echo pluginLang::plts('使用者名稱');?></label></dt>
			<dd><input type="text" class="input-text" id="teamusername" name="teamusername" maxlength="32" size="25" /></dd><br class="clear" />
		</dl>
		<div class="button-box"><input type="button" class="login-button input-button" value="<?php echo pluginLang::plts('確認送出');?>" onclick="teamblogsignup();" /></div>
	</div>
	<?php
	$signuphtml = ob_get_contents();
	ob_end_clean();
	
	ob_start();
	?>
	.signupform {
		margin:0 5px;
		padding:0 5px;
		font-size: 0.92em;
		line-height: 1.5em;
	}
	.signupform h3 {
		line-height: 2.2em;
	}
	.signupform dt
	{
		float: left;
		width: 180px;
		margin: 3px 0 0 0;
	}
	.signupform dd
	{
	}
	.signupform input[type=text], .signupform input[type=password], .signupform select
	{
		width: 200px;
	}
	br.clear
	{
		clear: both;
	}
	<?php
	$signupcss = ob_get_contents();
	ob_end_clean();
	
	ob_start();
	?>
	function checkInputValue(value) {
		if ( value.replace(/(^\s*)|(\s*$)/gi,"")!='' && value!=0) return true;
			return false;
	}
	function email_check(email) {
		var at="@";
		var dot=".";
		var lat=email.indexOf(at);
		var lstr=email.length;
		var ldot=email.indexOf(dot);
		if (!checkInputValue(email)) return false;
		if (email.indexOf(at)==-1) return false;
		if (email.indexOf(at)==-1 || email.indexOf(at)==0 || email.indexOf(at)==lstr) return false;
		if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr) return false;
		if (email.indexOf(at,(lat+1))!=-1) return false;
		if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot) return false;
		if (email.indexOf(dot,(lat+2))==-1) return false;
		if (email.indexOf(" ")!=-1) return false;

		return true;
	}
	function multiblogsignup() {
		try {
			var blogLoginid = document.getElementById('blogloginid').value;
			var blogPassword = document.getElementById('blogre-password').value;
			var blogName = document.getElementById('blogname').value;
			var userName = document.getElementById('username').value;
			
			if(!checkInputValue(blogLoginid) || !email_check(blogLoginid)) { alert('<?php echo pluginLang::plts('請輸入登入帳號。');?>'); return false; }
			if(!checkInputValue(blogPassword) || !checkInputValue(document.getElementById('blogpassword').value) || blogPassword!=document.getElementById('blogpassword').value) { alert('<?php echo pluginLang::plts('密碼確認錯誤，請重新輸入。');?>'); return false; }
			if(!checkInputValue(blogName)) { alert('<?php echo pluginLang::plts('請輸入部落格識別名稱。');?>'); return false; }
			if(!checkInputValue(userName)) { alert('<?php echo pluginLang::plts('請輸入使用者名稱。');?>'); return false; }
			
			var requireString = "loginid="+encodeURIComponent(blogLoginid)+"&passwd="+encodeURIComponent(blogPassword)+"&blogname="+encodeURIComponent(blogName)+"&username="+encodeURIComponent(userName);
			var request = new HTTPRequest('POST', './multiblogsignup.php');
			request.onSuccess = function() {
				var msg = this.getText("/response/result");
				PM.removeRequest(this);
				PM.showMessage(msg,'center','middle');
			}
			request.onError = function() {
				var msg = this.getText("/response/result");
				PM.removeRequest(this);
				PM.showErrorMessage(msg,'center','middle');
			}
			request.send(requireString);
		} catch(e) {
		}
	}
	
	function teamblogsignup() {
		try {
			var teamblogselect = document.getElementById('teamblogselect').value;
			var teamLoginid = document.getElementById('teamloginid').value;
			var teamPassword = document.getElementById('teamre-password').value;
			var teamUsername = document.getElementById('teamusername').value;
			
			if(!checkInputValue(teamLoginid) || !email_check(teamLoginid)) { alert('<?php echo pluginLang::plts('請輸入登入帳號。');?>'); return false; }
			if(!checkInputValue(teamPassword) || !checkInputValue(document.getElementById('teampassword').value) || teamPassword!=document.getElementById('teampassword').value) { alert('<?php echo pluginLang::plts('密碼確認錯誤，請重新輸入。');?>'); return false; }
			if(!checkInputValue(teamUsername)) { alert('<?php echo pluginLang::plts('請輸入使用者名稱。');?>'); return false; }
			
			var requireString = "teamblog="+encodeURIComponent(teamblogselect)+"&loginid="+encodeURIComponent(teamLoginid)+"&passwd="+encodeURIComponent(teamPassword)+"&username="+encodeURIComponent(teamUsername);
			var request = new HTTPRequest('POST', './teamblogsignup.php');
			request.onSuccess = function() {
				var msg = this.getText("/response/result");
				PM.removeRequest(this);
				PM.showMessage(msg,'center','middle');
			}
			request.onError = function() {
				var msg = this.getText("/response/result");
				PM.removeRequest(this);
				PM.showErrorMessage(msg,'center','middle');
			}
			request.send(requireString);
		} catch(e) {
		}
	}	
	<?php
	$signupscript = ob_get_contents();
	ob_end_clean();
	flush();
	
	return array($signuphtml, $signupcss, $signupscript);
}

require ROOT . '/lib/piece/blog/begin.php';
$userSignUpManagerView = $skin->cover;
$signUpView = HC_AddSignUpBlogForm();

dress('cover_rep', $signUpView, $userSignUpManagerView);
dress('cover', $userSignUpManagerView, $view);

require ROOT . '/lib/piece/blog/end.php';
?>